﻿//-----------------------------------------------------------------------
// <copyright file="OrderLines.cs" company="Craig Longford (deltawolf7@gmail.com)">
//     Copyright (c) Craig Longford. All rights reserved.
// </copyright>
//---------------------------------------------------------------------

namespace Delta.Mercury.Business.Commerce
{
    using System;
    using System.Collections.Generic;
    using Delta.Data.DataExpress;

    /// <summary>
    /// Collection of order lines
    /// </summary>
    [Serializable]
    public class OrderLines : List<OrderLine>
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the OrderLines class
        /// </summary>
        public OrderLines()
        {
        }

        /// <summary>
        /// Initializes a new instance of the OrderLines class
        /// </summary>
        /// <param name="capacity">Capacity of the list</param>
        public OrderLines(int capacity)
            : base(capacity)
        {          
        }

        /// <summary>
        /// Initializes a new instance of the OrderLines class
        /// </summary>
        /// <param name="collection">Collection of OrderLine</param>
        public OrderLines(IEnumerable<OrderLine> collection)
            : base(collection)
        {         
        }
                  
        #endregion

        #region Load Method
        /// <summary>
        /// Load orders lines by order
        /// </summary>
        /// <param name="order">Order identifier</param>
        /// <returns>Collection of orderlines</returns>
        public static OrderLines Load(Order order)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("OrderGuid", order.Guid));
            return DataAccess.GetCollection<OrderLines, OrderLine>("LoadOrderLines", System.Data.CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Save Method
        /// <summary>
        /// Save order lines
        /// </summary>
        /// <param name="order">Order to link</param>
        public void Save(Order order)
        {
            foreach (OrderLine line in this)
            {
                if (line.OrderGuid == Guid.Empty)
                {
                    line.OrderGuid = order.Guid;
                }

                line.Save();
            }
        }
        #endregion

        #region Has Product Method
        /// <summary>
        /// Check if product is within order lines.
        /// </summary>
        /// <param name="product">Product to check</param>
        /// <returns>Qty found in order lines</returns>
        public int HasProduct(IProduct product)
        {
            foreach (OrderLine line in this)
            {
                if (line.ProductGuid == product.Guid)
                {
                    return line.Qty;
                }
            }

            return 0;
        }
        #endregion

        #region Add Method
        /// <summary>
        /// Add an order line to collection
        /// </summary>
        /// <param name="newLine">New order line to add</param>
        public new void Add(OrderLine newLine)
        {
            foreach (OrderLine line in this)
            {
                if (line.ProductGuid == newLine.ProductGuid)
                {
                    line.Qty += newLine.Qty;
                    return;
                }
            }

            base.Add(newLine);
        }
        #endregion
    }
}
